<?php
if($_POST) {

    // Load the classes and create the new objects
    require_once('includes/core_class.php');
    require_once('includes/database_class.php');

    $core = new Core();
    $database = new Database();

    // Validate the post data
    if($core->validate_post($_POST) == true)
    {
        // First create the database, then create tables, then write config file
        if($database->create_database($_POST) == false)
        {
            $message = $core->show_message('error', "The database could not be created, please verify your settings.");
        }
        $core->write_config($_POST);
        
        // If no errors, redirect to registration page
        if(!isset($message)) {
            $redir = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
            $redir .= "://".$_SERVER['HTTP_HOST'];
            $redir .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
            $redir = str_replace('initialize/','',$redir);
            header('Location: ' . $redir . 'verification');
        }
    }
    else {
        $message = $core->show_message('error','Not all fields have been filled in correctly. The host, username, and database name are required.');
    }
}

$db_config_path = '../system/application/config/database.php';

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Install</title>
        <link rel="stylesheet" type="text/css" href="../css/install.css" />
        <link rel="stylesheet" type="text/css" href="../css/global.css" />
    </head>
    <body>
        <div id="container">
            <div id="leftcon">
                <div id="logo"></div>
            </div>
            <div id="content">
                <div id="install_content">

                    <h1>Database Settings</h1>
                    <form action="" method="post">
                    <label>
                        Host Name
                    </label>
                    <input type="text" id="host_name" name="hn" value="localhost" />
                    <label>
                        User Name
                    </label>
                    <input type="text" id="user_name" name="un" />
                    <label>
                        Password
                    </label>
                    <input type="password" id="pword" name="pw" />
                    <label>
                        Database Name
                    </label>
                    <input type="text" id="db_name" name="db" />
                    <button type="submit">
                        Submit
                    </button>
                    </form>
                    <?php
                        if(isset($message)) {
                    ?>
                    <div id="error">
                        <?php
                            echo $message;
                        ?>
                    </div>
                    <?php
                        }
                    ?>
                    <div id="note_spc"></div>

                </div>
            </div>
        </div>
    </body>
</html>